fix for REV-647 and some minor refactoring