Cleanup permalink generation

Fix permalink generation to not contain various smells. See verbose description in the last commit in this PR.
@dkubb Already fixed locally as part of implementing other suggested changes. But good catch. (I hate that duck typing allowed me to do this error).