Added error handling for failed queries (not finished).