Fixing some leaks and performance.

Uses some glib defines toSupport older glib versions.