Fix for various non obvious bugs.