Fixes in response to alexeykudinkin comments that it was O(n*m) not
O(n+m). Might need a tiny bit of tidying to clean up generics and
varagrs