added AST, cleaned up implementation