replaced goto statements with recursive functions