Test case added and some refactoring