Unit tests and minor fixes