combined duplicate code into functions