splitting the spaghetti code into functions