refactored code that checks if two tiles should be joined.