refactoring to simplify code