fix typo and a few optimizations