Refactor main into 4 other functions.