Cleanup permalink generation

Fix permalink generation to not contain various smells. See verbose description in the last commit in this PR.
@dkubb As we only count when an duplicate got generated and the chance for generating 2 duplicates in a row is that small I think adding the cache is not really worth it.