fixes and a bit more refactoring