Refactoring the code to ease the maintenance.

With derived functional changes from safetymapsDBK.