Added error handling for SQL failures