Cleaned up error handling, added logging to query