made improvements to allow for recursion and more robust if statements