Fixed faulty generation of surrogate ids

Closes #771