finish addpaper function and add defensive code