Fixed multiple encoding bugs