small refactoring and bug fixing