Split main into functions, add prototypes to header file