clean some sql queries and add some custom error messages