Added better class for distancemaps. This should hopefully work better
than before. Will be applied in next commit.
Minor optimizations here and there.