diff --git a/Data/CRF/Chain1/Constrained/Dataset/Codec.hs b/Data/CRF/Chain1/Constrained/Dataset/Codec.hs
--- a/Data/CRF/Chain1/Constrained/Dataset/Codec.hs
+++ b/Data/CRF/Chain1/Constrained/Dataset/Codec.hs
@@ -1,6 +1,8 @@
 module Data.CRF.Chain1.Constrained.Dataset.Codec
 ( Codec
 , CodecM
+, obMax
+, lbMax
 
 , encodeWord'Cu
 , encodeWord'Cn
@@ -40,6 +42,18 @@
 -- of type a, the second one is used to encode labels of type b.
 type Codec a b = (C.AtomCodec a, C.AtomCodec (Maybe b))
 
+-- | The maximum internal observation included in the codec.
+obMax :: Codec a b -> Ob
+obMax =
+    let idMax m = M.size m - 1
+    in  Ob . idMax . C.to . fst
+
+-- | The maximum internal label included in the codec.
+lbMax :: Codec a b -> Lb
+lbMax =
+    let idMax m = M.size m - 1
+    in  Lb . idMax . C.to . snd
+
 -- | The empty codec.  The label part is initialized with Nothing
 -- member, which represents unknown labels.  It is taken on account
 -- in the model implementation because it is assigned to the
@@ -106,7 +120,7 @@
     return $ mkX x' r'
 
 -- | Encode the word and do *not* update the codec.
-encodeWord'Cn :: (Ord a, Ord b) =>Word a b -> CodecM a b X
+encodeWord'Cn :: (Ord a, Ord b) => Word a b -> CodecM a b X
 encodeWord'Cn word = do
     x' <- catMaybes <$> mapM encodeObN (S.toList (obs word))
     r' <- mapM encodeLbN (S.toList (lbs word))
diff --git a/Data/CRF/Chain1/Constrained/Dataset/Internal.hs b/Data/CRF/Chain1/Constrained/Dataset/Internal.hs
--- a/Data/CRF/Chain1/Constrained/Dataset/Internal.hs
+++ b/Data/CRF/Chain1/Constrained/Dataset/Internal.hs
@@ -92,6 +92,8 @@
 -- potential labels for corresponding 'X' word.
 -- TODO: Perhaps we should substitute 'Lb's with label indices
 -- corresponding to labels from the vector of potential labels?
+-- FIXME: The type definition is incorrect (see 'fromList' definition),
+-- it should be something like AVec2.
 newtype Y = Y { _unY :: AVec (Lb, Double) }
     deriving (Show, Read, Eq, Ord)
 
diff --git a/Data/CRF/Chain1/Constrained/Inference.hs b/Data/CRF/Chain1/Constrained/Inference.hs
--- a/Data/CRF/Chain1/Constrained/Inference.hs
+++ b/Data/CRF/Chain1/Constrained/Inference.hs
@@ -106,7 +106,7 @@
         | i == 0    = (0, 0)
         | otherwise = (0, lbNum crf xs (i-1) - 1)
     withMem psi beta i
-        | i == V.length xs = const 0
+        | i == V.length xs = const 1
         | i == 0 = const $ sum
             [ beta (i+1) k * psi k
             * sgValue crf (lbOn crf (xs V.! i) k)
diff --git a/Data/CRF/Chain1/Constrained/Model.hs b/Data/CRF/Chain1/Constrained/Model.hs
--- a/Data/CRF/Chain1/Constrained/Model.hs
+++ b/Data/CRF/Chain1/Constrained/Model.hs
@@ -92,8 +92,8 @@
 -- the set of observations is of the {0, 1, .. 'obMax'} form.
 -- There should be no repetition of features in the input list.
 -- TODO: We can change this function to take M.Map Feature Double.
-fromList :: [(Feature, Double)] -> Model
-fromList fs =
+fromList :: Ob -> Lb -> [(Feature, Double)] -> Model
+fromList obMax' lbMax' fs =
     let _ixMap = M.fromList $ zip
             (map fst fs)
             (map FeatIx [0..])
@@ -102,10 +102,13 @@
         tFeats = [feat | (feat, _val) <- fs, isTFeat feat]
         oFeats = [feat | (feat, _val) <- fs, isOFeat feat]
 
-        obMax = (unOb . maximum . Set.toList . obSet) (map fst fs)
-        lbs   = (Set.toList . lbSet) (map fst fs)
-        lbMax = (unLb . maximum) lbs
-        _r0   = A.fromList lbs
+        obMax = unOb obMax'
+        lbMax = unLb lbMax'
+        _r0   = A.fromList (map Lb [0 .. lbMax])
+        -- obMax = (unOb . maximum . Set.toList . obSet) (map fst fs)
+        -- lbs   = (Set.toList . lbSet) (map fst fs)
+        -- lbMax = (unLb . maximum) lbs
+        -- _r0   = A.fromList lbs
         
         _sgIxsV = sgVects lbMax
             [ (unLb x, featToJustIx crf feat)
@@ -125,13 +128,13 @@
 
         -- | Adjacency vectors.
         adjVects n xs =
-            V.replicate n (A.fromList []) V.// update
+            V.replicate (n + 1) (A.fromList []) V.// update
           where
             update = map mkVect $ groupBy ((==) `on` fst) $ sort xs
             mkVect (y:ys) = (fst y, A.fromList $ map snd (y:ys))
             mkVect [] = error "mkVect: null list"
 
-        sgVects n xs = U.replicate n dummyFeatIx U.// xs
+        sgVects n xs = U.replicate (n + 1) dummyFeatIx U.// xs
 
         _values = U.replicate (length fs) 0.0
             U.// [ (featToJustInt crf feat, val)
@@ -139,33 +142,33 @@
         crf = Model _values _ixMap _r0 _sgIxsV _obIxsV _prevIxsV _nextIxsV
     in  crf
 
--- | Compute the set of observations.
-obSet :: [Feature] -> Set.Set Ob
-obSet =
-    Set.fromList . concatMap toObs
-  where
-    toObs (OFeature o _) = [o]
-    toObs _              = []
-
--- | Compute the set of labels.
-lbSet :: [Feature] -> Set.Set Lb
-lbSet =
-    Set.fromList . concatMap toLbs
-  where
-    toLbs (SFeature x)   = [x]
-    toLbs (OFeature _ x) = [x]
-    toLbs (TFeature x y) = [x, y]
+-- -- | Compute the set of observations.
+-- obSet :: [Feature] -> Set.Set Ob
+-- obSet =
+--     Set.fromList . concatMap toObs
+--   where
+--     toObs (OFeature o _) = [o]
+--     toObs _              = []
+-- 
+-- -- | Compute the set of labels.
+-- lbSet :: [Feature] -> Set.Set Lb
+-- lbSet =
+--     Set.fromList . concatMap toLbs
+--   where
+--     toLbs (SFeature x)   = [x]
+--     toLbs (OFeature _ x) = [x]
+--     toLbs (TFeature x y) = [x, y]
 
 -- | Construct the model from the list of features.  All parameters will be
 -- set to 0.  There can be repetitions in the input list.
 -- We assume that the set of labels is of the {0, 1, .. 'lbMax'} form and,
 -- similarly, the set of observations is of the {0, 1, .. 'obMax'} form.
-mkModel :: [Feature] -> Model
-mkModel fs =
+mkModel :: Ob -> Lb -> [Feature] -> Model
+mkModel obMax lbMax fs =
     let fSet = Set.fromList fs
         fs'  = Set.toList fSet
         vs   = replicate (Set.size fSet) 0.0
-    in  fromList (zip fs' vs)
+    in  fromList obMax lbMax (zip fs' vs)
 
 -- | Model potential defined for the given feature interpreted as a
 -- number in logarithmic domain.
diff --git a/Data/CRF/Chain1/Constrained/Train.hs b/Data/CRF/Chain1/Constrained/Train.hs
--- a/Data/CRF/Chain1/Constrained/Train.hs
+++ b/Data/CRF/Chain1/Constrained/Train.hs
@@ -18,7 +18,7 @@
 import Data.CRF.Chain1.Constrained.Dataset.Internal
 import Data.CRF.Chain1.Constrained.Dataset.External (SentL, unknown, unDist)
 import Data.CRF.Chain1.Constrained.Dataset.Codec
-    (mkCodec, Codec, encodeDataL, encodeLabels)
+    (mkCodec, Codec, obMax, lbMax, encodeDataL, encodeLabels)
 import Data.CRF.Chain1.Constrained.Feature (Feature, featuresIn)
 import Data.CRF.Chain1.Constrained.Model
     (Model (..), mkModel, FeatIx (..), featToJustInt)
@@ -62,7 +62,7 @@
         Just evalIO -> Just . encodeDataL _codec <$> evalIO
         Nothing     -> return Nothing
     let feats = extractFeats _r0 trainData
-        crf = (mkModel feats) { r0 = _r0 }
+        crf = (mkModel (obMax _codec) (lbMax _codec) feats) { r0 = _r0 }
     para <- SGD.sgdM sgdArgs
         (notify sgdArgs crf trainData evalDataM)
         (gradOn crf) (V.fromList trainData) (values crf)
diff --git a/crf-chain1-constrained.cabal b/crf-chain1-constrained.cabal
--- a/crf-chain1-constrained.cabal
+++ b/crf-chain1-constrained.cabal
@@ -1,5 +1,5 @@
 name:               crf-chain1-constrained
-version:            0.1.0
+version:            0.1.1
 synopsis:           First-order, constrained, linear-chain conditional random fields
 description:
     The library provides efficient implementation of the first-order,
