Refactor parser and add tests