Maybe these three lines should be factored out? I seems to be a common check.