Loads of misc code cleanup.