code updated with close button functionality