Error tests and bug fixes