fixes and refactoring