Cleanup permalink generation

Fix permalink generation to not contain various smells. See verbose description in the last commit in this PR.
Since you are testing less than and more than, should you also be testing the `equal to` case?