Reorganized code to separate functions into sections