Cleaning code to functions