Revised the next function