Ensure unique id assignment

6c171fb4b9eb813c0b4ecc8974b6f011618ad167 introduced a bug in which the `$unique_id` variable was not getting set before it was getting called.