Added test cases and code refactor