Refactored outerProduct code