few more comments and refactored out some duplicate code