refactored into 2 pieces: main and wd (wp is wip)