Move redundant code to own functions