Some more code complexity changes. Abstract some stuff out, and an undefined check.