refactoring to minimize code