Replaced backticks with dollar+brackets
Removed useless sleep command
improved compatibility with unusual file/directory names
Cleaned up encode code