refactoring + fix bug