Fix tests and a bug