rewrote rl_emul gap buffering, slightly improved error handling