Massive refactoring - trying to move all the js code to a set of separate directives. Fixes #19