Simplify insanely nested conditional ifs structure