Added some comments, minor refactoring