Refactored main.c and removed unused funtionality

Cleaned up main method in main.c and factored out
some of the functionality to functions to
improve readability of the code.