Fixed preconditions for encode function
Added comments