Various cleanups

First part of a large cleanup. This portion contains bug fixes, removal of dead code and other (hopefully) safe changes.