Added better exception handling. Refactored a lot of the code.

Look, this doesn't work at the moment - I did a lot. It still doesn't work.  Get off my back....