Unit tests and fixes