Adding unit tests and fixing the stuff