Overhaul linear algebra code