More tests and bug fixes