Cleanup permalink generation

Fix permalink generation to not contain various smells. See verbose description in the last commit in this PR.
Looks like an extra blank line here, compared to other code in this spec.