Moved the options control code to a function outside __main__