fixed recursive code