Improvements to correctly handle special characters
Remove redundant code and simplify the script