Merge script bugfix and updated sql