Add options, bugfixes and general code refactoring.