JavaScript refactoring
Improve horizontal mode design. Fixes #4