Memory fixes and major clean-up.