Single recursive function for calculating targets debugged and working