General fixes, improvements and refactoring.

Added support for command line argument passing.