Refactored contains() so it doesn't do near worst case