Miscellaneous bug fixes, null checks etc