Added commandline functionality, refactored some code.