Fixed dragging bug and refactored some code