refactoring, bug fixes