Add a utililty function to get next the available ID and deduplicate some code