There is a lot of redundant code. Is it maybe possible to extract the differences?