Improved exception handling and logging

Cleaned up unused code and improved exception handling and logging