improved memory handling