handle unexpected permalink; some refactor;