refactoring and small fixes