Fixed container_of to work with NULL pointers
Added check for foreach