Slight variation on Peter Ferrie's setmem()