reorganize the code related to extend

reorganize the code related to extend , make it more declarative