Danfixes

Work in progress. This combines Mark's fixes with some cleanup I've done.