Reformatted the main function and some functions