Cleanup and code improvement for device modes.