Clean up memory.rb (break definitions out) and fix String#replace