Complete code reorganization

MUCH cleaner than before on the Java side. The JavaScript side still sucks,
however.