refactored chain matching into chain_match.py