updated file to work with inverted matrix