Fixed bug in encode function.