minor fixes and code refactoring