Imported improvements from Theraot and clean up