Tricky read algorithm but seems to work