trying to implement this using sparse matrices