Implemented the "back" button logic