refactored and cleaned up according to code review comments