Fixed the errors and warnings in the nested loop