Move null check to the top