Improved memory management and bugfixes.