Big cleanup of the code

Added a lot of comments, removed commented code.