Rewrite r21464 in a more compact/readable way. Reduces code duplication as well.