Refactoring and js bug