refactor for requireJS, much cleaner now