Cleans up the code.

Removes unnecessary code and adds comments to large sections of code.