Convert code to functions