Unit Testing added and minor refactoring