Unit tests and fixes for them