code for toAST is shorter and easier to understand now