Moves code in to relevant functions