Refactored to be stateless. Now uses regexp pattern matching.