encoding and error handling