added length check on serialization