many fixes and minor refactorings