Improvements/bugfixes to new SQLWrite code.