All this weird searching logic must be separated and refactored.