Since you are using recursion(having a function call itself), the lag probably occurs because the function calls itself multiple times, and then those new functions call themself multiple times etc. causing the lag to just multiply. Could you format your code better so we can see where the recursion messes up. To format your code do three of these ` (the key below the esc key).
--```
--code here
--```