fixed more bugs in sql queries