refactored to use regexp pattern matching