Bugfix and preliminary SQL