rewrite cardinality method to not use optional so it can be optimised
easier