Added safety check to element reference