added a bit of SQL error handling