merge and refactor upstream changes