Refactor to use S-expressions and pointer passing