fix typo and restructure code a bit.