Added comments, minor refactoring