Some logic flattening and cleanup.