Fixed bug in encoding