Rewrite a messy algorithm

THEORETICAL RECAP An algorithm is a finite, ordered sequence of unambiguous instructions designed to solve a specific problem. The key word here is 'ordered': each step must follow logically from the previous one, and every instruction must be clear ...