Added utility func to handle non-square matrix and added additional comments