The filtering and length check are because otherwise it would write `[]` when the length was zero and `null` as a statement when the statement was null.