Proper argument parsing, plus some tidying.