Fixed code to damaging bugs