fix typos
improve error handling and efficiency