Fixed memory leak.
Various small tweaks and bug fixes.