Unit tests added, minor refactoring