Added onclose handler for cleanup