Fix some minor bugs in the contains implementation. Refactor