refactoring, improved logging