imported code from aospa-legacy