fixed encoding errors, better error handling