Multiple fixes

Catching all uncaught exceptions.
Added logging to file.
Refactored and cleaned up.