Fixed recursive function bug