refactored some code around regex matching