Fixed SQL issues