Unit tests added and some bug fixes