Moved the print and $endpoint decision outside of the function.