Avoiding recursive loop