cleaned up TryGetElement code