Refactored to eliminate (almost) identical R/W code.