Working from pull req feedback, including refactoring code and other minor fixes