Used string.encode to remove any malformed UTF-8 strings, as they were causing the code to crash.