Added null reference checks because of broken API