this is hard to follow, please split into multiple ifs