Refactored match(...) to use start and end indices