updated to handle y recursion