Prevent duplication of short links

Prevents duplication of short links. Along with some small tidy ups to the code.