Fixing NPEs and SQL bugs