optimize memory and clean up