refactoring + fix bugs