Fixed error where invalid ids can be generated by the script