Refactoring code, rewrite js