Back porting the paste feature from the main branch. Also making sure selectedId isnt globally leaked.