fix many of the bugs so it passes tests