Recurse directly rather than via foldr
expose clearer match results, switch a recursion for foldr, few general improvements
prefer library functions to pattern matching