Some renaming and refactoring. Implemented finding nearest ancestor with
same Amem.