fix bug in rebuild_case and add test that would have caught it