Cleaned up and organized code

Cleaned up and organized code. Added some comments explaining some
things.