Fixed case mismatch problem; used else clause to make loop breaking pythonic; made character checking O(1) instead of O(n)