unit tests added and some function refined