added proper cleanup beforeunload