refactoring on middleend and more