Refactor to store user ap_id, add tests