various fixes and refactorings