Fixes for some really buggy code...