Added checks to avoid notices of undefined variables