A little refactoring, plus improve error locations