fix for null ref & also fixes some other small issues