Trying to fix: AttributeError: 'NoneType' object has no attribute 'encode'