Altered tie mechanism to use a double-nested for loop

I had actually tried to do this before I submitted my previous pull request, but failed. I gave it another go after reading your comment, and I think it works great!