minor code fixes and refactoring