Rearranged some things for clarity, improved some variable names and comments, added some additional comments.