How to fix a problem with dublicated code?