Incorporated code review suggestions. Made a helper function and called that from 3 different places, incorporating the subtle differences b/w different places.

Quality fixes.