refactoring and small fixes.