Sqloptimizations stable

Just two changes remained after testing, I was a bit too enthousiastic on the earlier branch.