implementing #11, refactored to share code with match all