minor fixes and refactoring