Code cleanup for merging upstream