Refactored main.c into more reasonable sections