Added some checking and fixes.