Fixed resursion function bug