Minor cleanup and logic restructuring.