Refactor AS, MMAS, extracting C functions