refactor Cartesian product code to a sub

this should fix #175