Properly parse command-line arguments

And also do some other cleanup