slight reorganization of code for clearity