Added comments, did some refactoring.