Large-scale rewrite for code readability