fixing bugs to keep state consistent