some fixes and refactoring