Moved w_mem update logic to a separate process. Minor cleanup.