Extracted out matrix code into sperate func