Added logging, and better exception handling