moved getNextFreeId to a template