Unit tests and associated fixes