implements recusiv function