improving to handle 2 and 3 col matrices