Restructure the coding, and fixed the next and previous button function