Changed the way ID are collected.

All referenced IDs are collected and mapped to unique IDs. Unreferenced IDs are removed in order to save space and clutter. Furthermore, the md5()-function is only called when really necessary which leads to (micro) time optimizations.