Cleanup permalink generation

Fix permalink generation to not contain various smells. See verbose description in the last commit in this PR.
Is it necessary to copy the implementation here? Wouldn't it be possible just to create a candidate and use that in the spec?