Fix the bugs spotted by Novatux - thanks for spotting them