Refactored a lot. Improved exception handling considerably.