refactored main.c so the main function isn't so big