Refactor checkTie function