Fix error handling and cleanup match loop.