Clearer pseudocode for main logic