updated queries to catch all the null cases, and other bugs. Also added script