changed bit shifting to include prior state