Unit tests and relevant fixes