In my experience accidentally leaving the _MCLR pin floating causes erratic behaviour such as yours. Make sure it is tied to +5V through a resistor (47kΩ or so), in order to prevent the PIC from randomly resetting.