fix encoding bug and better error handling