Fix tests and bugs