DONE: refactoring and bug fixes