Refactor main function and add testing