fixed a bug in encode