You don't merge anything. You use the code and the two functions I originally posted in place of your code.