Code cleanup, comments, minor refactoring