Refactor routing code, add permalinks