refactor the parsing and add doctest