Breaking the JS into modular sections

Fixes #339.