refactoring and some minor fixes