refactoring complete for js