Minor improvements for performance and memory consumption. Also fixed one leak.