rewite some more SQL (untested)