Improvements to ap.c