Some comments and stylistic changes.

I cleaned it up a bit, and added some comments on the end. Look at the two side by side to see how my version is a little easier to read through. Yours was definitely correct though.