add output messages to improved algorithm for clarity