refectored to avoid ifs