Move some code around and fix some crashes related to drag&drop