This seems to work for the most common usecases.