Minor cleanup, added comments, some refactoring