factor out null/undefined check