Added commandline options and parsing.

Also refactored some code.