Refactor alignment code for fewer nested ifs