Refactored checking for end in keys.