working on refactoring the logic