Better tests and bug fixes