Refactored Logic

and create a Junit test class for Integration testing