Remove code duplication

As #91 seems to be rejected, I'd like to propose to get this PR merged in. It removes some code duplication.

Thanks in advance.