Improved tests and code