extensive code cleanup (no actual feature change intended)