[IMP] Simplified a bit the nested ifs (as suggested by @gdgellatly)