Refactor and improve pasting related utilties