add ad and refactor a bit