Do a more complete object type check to catch case that elem is undefined