Code merge from local dev version.

This is a big ugly unstructured commit in preparation for an upcoming
major UI overhaul, but right now nobody else is expected to work with
the code base anyway. There might be database changes that I forgot to
include.