Moved exception handling and added some logs.