misc fixes and refactoring