Tidied some function calls
Added recursive algorithm