Unit tests + various fixes