Refactored some drag and drop code

Could still be further refined.