refactored code a little to be more intelligible.  Much of the changes I made didn't actually work though, so I had to undo them.