minor refactoring, added logging