combine some repeated code

Not an essential change, but I think it's an improvement.