afm: move afm-related functions to afm.c