This method is really long, complex, full of ifs, and probably should be refactored