code refactoring to avoid duplication