It will come with practice...
I think youre doing it right... first, get it working the way you want. Then we can work on refactoring your code and making it "pretty." It's hard when you are plopped into advanced (heck, Im not even that advanced) code, without knowing the basics of what it is doing.
Did you find the error in the place I said? Or is it somewhere else?