Several fixes

Fixed postgres sql wrapping, as well as empty handler bug and a couple of other minor things.
See individual commits for details.