Remove code duplication (by heinrich5991)