some fixes and refactorings