 
   override func get() -> T?
   {
-    if self.closed && elements <= 0 { return nil }
-
-    dispatch_semaphore_wait(mutex, DISPATCH_TIME_FOREVER)
-
-    while !self.closed && elements <= 0
+    if self.closed && head >= tail { return nil }
+
+    dispatch_semaphore_wait(mutex, DISPATCH_TIME_FOREVER)
+
+    while !self.closed && head >= tail
     {
       wait(mutex: mutex, queue: readerQueue)
-      dispatch_semaphore_wait(mutex, DISPATCH_TIME_FOREVER)
-    }
-
-    if self.closed && elements <= 0
+    }
+
+    if self.closed && head >= tail
     {
       dispatch_semaphore_signal(mutex)
       return nil
     }
 
-    let oldElement = q.dequeue()
-    elements -= 1
+    let element = headptr.move()
+    headptr = headptr.successor()
+    head += 1
+    if (head&mask == 0) { headptr = buffer }
 
     if writerQueue.count > 0
     {
       dispatch_semaphore_signal(writerQueue.dequeue()!)
     }
 
-    if elements > 0 && readerQueue.count > 0
+    if head < tail && readerQueue.count > 0
     {
       dispatch_semaphore_signal(readerQueue.dequeue()!)
     }
 
     dispatch_semaphore_signal(mutex)
 
-    return oldElement
+    return element
   }
 }


    dispatch_semaphore_wait(mutex, DISPATCH_TIME_FOREVER)

    while !self.closed &&  elements >= capacity
    {
      wait(mutex: mutex, queue: writerQueue)
      dispatch_semaphore_wait(mutex, DISPATCH_TIME_FOREVER)
    }

    if self.closed

      return false
    }

    tailptr.initialize(newElement)
    tailptr = tailptr.successor()
    tail += 1
    if (tail&mask == 0) { tailptr = buffer }

    if readerQueue.count > 0
    {
      dispatch_semaphore_signal(readerQueue.dequeue()!)
    }

    if head+capacity < tail && writerQueue.count > 0
    {
      dispatch_semaphore_signal(writerQueue.dequeue()!)
    }
