code review and refactor, bug fixes