Add missing encoding and error handling