Edited loop and ifs