refactored code and added uri encoding permalink