Lots of refactoring

Taking advantage of being in Scala, which lets me much more easily
refactor this bloody UI code.

Introduced lots of utility functions to support refactoring, and to
begin to standardize everything better.