improved encode