added max_mismatch heuristic; wrote big loop