 }
 
 /**
-Restarts transaction. 
+Restarts transaction.
 */
-public func retry() -> STM<()> {
-    return STM({return ((), _retry($0)) })
-}
-
+public func retry<A>() -> STM<A> {
+    return STM.ret(returnM(nil))
+}
 
 //---------------------//
 //MARK: Private section
 //---------------------//
+
+private class _STM<A> {
+    private var run: (Transactions -> (A, Transactions))
+    
+    private init(_ x: A) {
+        run = {return (x, $0)}
+    }
+    
+    private init(_ f: Transactions -> (A, Transactions)) {
+        run = f
+    }
+    
+    private class func ret(val:A) -> _STM {
+        return _STM(val)
+    }
+
+}
+
+private func returnM<A> (x: A) -> _STM<A> {
+    return _STM.ret(x)
+}
+
+private func >>>= <A,B> (processor: _STM<A>, processorGenerator: (A -> _STM<B>)) -> _STM<B> {
+    return _STM( {st -> (B, Transactions) in
+        let (x, st1) = processor.run(st)
+        return processorGenerator(x).run(st1)
+    })
+}
+
+private func >>> <A,B> (processor: _STM<A>, processorGenerator: (() -> _STM<B>)) -> _STM<B> {
+    return _STM( {st -> (B, Transactions) in
+        let (_, st1) = processor.run(st)
+        return processorGenerator().run(st1)
+    })
+}
+
+private func _newTVarSTM<T: TVarProtocol>(val: T) -> _STM<TVar<T>> {
+    return returnM(TVar(val))
+}
+
+private func _newTVarSTM<T>(val: [T]) -> _STM<TVarArray<T>> {
+    return returnM(TVarArray(val))
+}
+
+private func _newTVarSTM<K,V>(val: [K:V]) -> _STM<TVarDictionary<K,V>> {
+    return returnM(TVarDictionary(val))
+}
+
+//MARK: readTVar
+private func _readTVar<T: TVarProtocol>(tvar: TVar<T>) -> _STM<T?> {
+    return _STM({ __readTVar(tvar, $0) })
+}
+
+private func _readTVar<T>(tvar: TVarArray<T>) -> _STM<[T]?> {
+    return _STM({ __readTVar(tvar, $0) })
+}
+
+private func _readTVar<K, V>(tvar: TVarDictionary<K, V>) -> _STM<[K:V]?> {
+    return _STM({ __readTVar(tvar, $0) })
+}
+
+//MARK: writeTVar
+private func _writeTVar<T: TVarProtocol> (tvar: TVar<T>, val: T) -> _STM<()?> {
+    return _STM({ ((), __writeTVar(tvar, val, $0)) })
+}
+
+private func _writeTVar<T> (tvar: TVarArray<T>, val: [T]) -> _STM<()?> {
+    return _STM({ ((), __writeTVar(tvar, val, $0)) })
+}
+
+private func _writeTVar<K,V> (tvar: TVarDictionary<K,V>, val: [K:V]) -> _STM<()?> {
+    return _STM({ ((), __writeTVar(tvar, val, $0)) })
+}
 
 private protocol _TVarPrivateProtocol {
     // Looks like a dirty hack? so it is ;)

}

public func >>>= <A,B> (processor: STM<A>, processorGenerator: (A -> STM<B>)) -> STM<B> {
    return STM( {st -> (B, Transactions) in
        let (x, st1) = processor.run(st)
        return processorGenerator(x).run(st1)
    })
}

infix operator  >>> {

    
    if let (readLog, writeLog) = trans.log[id]  {
        if let val = writeLog {
            return ((val as [T]), trans)
        } else {
            let copy = tvar.value.copy() as [T]
            trans.log[id] = (copy, writeLog)
