Simplified code a bit -- no significant change in behavior expected