Changed indentation, added comments, fixed a few bugs.