I wonder if those 4 nested levels of ifs are really necessary..