Moved common code to modules

Added support for MPAO using DEFINE.