implemented recursive function