Lots of changes to get tests passing (fix bugs)