Fixed scope problems and "return undef" mis-idiom