Refacotred the variables to account for recursive solution