Some refactorings and improvements for AP state handling.