Fix tests and two bugs