Move the undefined check to the right place