Refactor main for error handling.