Refactor: used a loop rather than repeated ifs