completed and tested tail recursive partition