Major clean up

Added a lot of comments, and tried my best to clean up the code.