Some Refactoring and Unit Tests added