finished matrix version