fix max_end routine