Cleanup permalink generation

Fix permalink generation to not contain various smells. See verbose description in the last commit in this PR.
Does incrementing the `length` like this actually modify `self.length`, or does a local variable get auto-vivified and then incremented?