This is just coming from a novice but I don't think "g2" was defined in your script. You have to initialize it first which I don't see anywhere in there.