Add some error guards

Also fix setting cursor position on return to buffer, now that the try
catch block makes that bug obvious.