changed reverse to tail recursive