Introduce FunctionBlock tree and refactor to share more code