refactoring suggested by etrepum