refactoring to simplify the code