Main into smaller functions, adding -o option.