Slightly experimental cleanup of some ugly code.