Not that this is terribly complicated code, but do we have any reasonable way to avoid code duplication here?