Bunch of fixes
Fix sql