diff --git a/Data/ByteString/Base64/Internal.hs b/Data/ByteString/Base64/Internal.hs
--- a/Data/ByteString/Base64/Internal.hs
+++ b/Data/ByteString/Base64/Internal.hs
@@ -111,6 +111,7 @@
 -- > joinWith "|" 2 "----" = "--|--|"
 --
 -- > joinWith "\r\n" 3 "foobarbaz" = "foo\r\nbar\r\nbaz\r\n"
+-- > joinWith "x" 3 "fo" = "fox"
 joinWith :: ByteString  -- ^ String to intersperse and end with
          -> Int         -- ^ Interval at which to intersperse, in bytes
          -> ByteString  -- ^ String to transform
@@ -124,21 +125,28 @@
     withForeignPtr bfp $ \bptr -> do
       withForeignPtr sfp $ \sptr -> do
           let bp = bptr `plusPtr` boff
-          let dEnd = dptr `plusPtr` dlen
-              loop !dp !sp | dp >= dEnd = return ()
-                           | otherwise = do
-                let n = min every (dEnd `minusPtr` dp)
-                memcpy dp sp (fromIntegral n)
-                memcpy (dp `plusPtr` n) bp (fromIntegral blen)
-                loop (dp `plusPtr` (n + blen)) (sp `plusPtr` every)
-          loop dptr (sptr `plusPtr` soff)
-  where dlen = slen + blen * numBreaks
+              sp0 = sptr `plusPtr` soff
+              sLast = sp0 `plusPtr` (every * numBreaks)
+              loop !dp !sp
+                  | sp == sLast = do
+                      let n = sp0 `plusPtr` slen `minusPtr` sp
+                      memcpy dp sp (fromIntegral n)
+                      memcpy (dp `plusPtr` n) bp (fromIntegral blen)
+                  | otherwise = do
+                memcpy dp sp (fromIntegral every)
+                let dp' = dp `plusPtr` every
+                memcpy dp' bp (fromIntegral blen)
+                loop (dp' `plusPtr` blen) (sp `plusPtr` every)
+          loop dptr sp0
+  where dlast = slen + blen * numBreaks
+        dlen | slen `mod` every > 0 = dlast + blen
+             | otherwise            = dlast
         numBreaks = slen `div` every
 
 -- | Decode a base64-encoded string.  This function strictly follows
 -- the specification in RFC 4648,
 -- <http://www.apps.ietf.org/rfc/rfc4648.html>.
--- This function takes the decoding table (for @base64@ or @base64url@) as 
+-- This function takes the decoding table (for @base64@ or @base64url@) as
 -- the first paramert.
 decodeWithTable :: ForeignPtr Word8 -> ByteString -> Either String ByteString
 decodeWithTable decodeFP (PS sfp soff slen)
@@ -195,7 +203,7 @@
 -- following the specification from RFC 4648,
 -- <http://www.apps.ietf.org/rfc/rfc4648.html>, and will not generate
 -- parse errors no matter how poor its input.
--- This function takes the decoding table (for @base64@ or @base64url@) as 
+-- This function takes the decoding table (for @base64@ or @base64url@) as
 -- the first paramert.
 decodeLenientWithTable :: ForeignPtr Word8 -> ByteString -> ByteString
 decodeLenientWithTable decodeFP (PS sfp soff slen)
diff --git a/base64-bytestring.cabal b/base64-bytestring.cabal
--- a/base64-bytestring.cabal
+++ b/base64-bytestring.cabal
@@ -1,5 +1,5 @@
 name:                base64-bytestring
-version:             0.1.1.3
+version:             0.1.2.0
 synopsis:            Fast base64 encoding and deconding for ByteStrings
 description:         Fast base64 encoding and deconding for ByteStrings
 homepage:            https://github.com/bos/base64-bytestring
diff --git a/tests/Tests.hs b/tests/Tests.hs
--- a/tests/Tests.hs
+++ b/tests/Tests.hs
@@ -7,14 +7,14 @@
 import Test.Framework.Providers.QuickCheck2 (testProperty)
 import Test.Framework.Providers.HUnit (testCase)
 
-import Test.QuickCheck (Arbitrary(..))
+import Test.QuickCheck (Arbitrary(..), Positive(..))
 
 import Control.Monad (liftM)
 import qualified Data.ByteString.Base64 as Base64
 import qualified Data.ByteString.Base64.URL as Base64URL
 import Data.ByteString (ByteString)
 import Data.ByteString.Char8 ()
-import qualified Data.ByteString as B
+import qualified Data.ByteString.Char8 as B
 import Test.HUnit hiding (Test)
 
 
@@ -22,14 +22,18 @@
 main = defaultMain tests
 
 tests :: [Test]
-tests = [ 
-    testGroup "Base64" [
+tests = [
+    testGroup "joinWith" [
+        testProperty "all_endsWith" joinWith_all_endsWith
+      , testProperty "endsWith" joinWith_endsWith
+    ]
+  , testGroup "Base64" [
         testProperty "decodeEncode" $
           genericDecodeEncode Base64.encode Base64.decode
       , testProperty "decodeEncode Lenient" $
           genericDecodeEncode Base64.encode
                               (liftM Right Base64.decodeLenient)
-      , testGroup "base64-string tests" base64_string_tests 
+      , testGroup "base64-string tests" base64_string_tests
     ]
   , testGroup "Base64URL" [
         testProperty "decodeEncode" $
@@ -37,13 +41,28 @@
       , testProperty "decodeEncode Lenient" $
           genericDecodeEncode Base64URL.encode
                               (liftM Right Base64URL.decodeLenient)
-      , testGroup "base64-string tests" base64url_string_tests 
+      , testGroup "base64-string tests" base64url_string_tests
     ]
   ]
 
 instance Arbitrary ByteString where
   arbitrary = liftM B.pack arbitrary
 
+joinWith_endsWith :: ByteString -> Positive Int -> ByteString -> Bool
+joinWith_endsWith brk (Positive int) str =
+  brk `B.isSuffixOf` Base64.joinWith brk int str
+
+chunksOf :: Int -> ByteString -> [ByteString]
+chunksOf k s
+  | B.null s  = []
+  | otherwise = let (h,t) = B.splitAt k s
+                in h : chunksOf k t
+
+joinWith_all_endsWith :: ByteString -> Positive Int -> ByteString -> Bool
+joinWith_all_endsWith brk (Positive int) str =
+    all (brk `B.isSuffixOf`) . chunksOf k . Base64.joinWith brk int $ str
+  where k = B.length brk + min int (B.length str)
+
 -- | Decoding an encoded sintrg should produce the original string.
 genericDecodeEncode :: (ByteString -> ByteString)
                     -> (ByteString -> Either String ByteString)
@@ -51,14 +70,14 @@
 genericDecodeEncode enc dec x = case dec (enc x) of
                                   Left  _  -> False
                                   Right x' -> x == x'
-  
+
 --
 -- Unit tests from base64-string
 -- Copyright (c) Ian Lynagh, 2005, 2007.
 --
 
 base64_string_tests :: [Test]
-base64_string_tests = 
+base64_string_tests =
   base64_string_test Base64.encode Base64.decode testData ++
   base64_string_test Base64.encode decodeURL testData
   where decodeURL :: ByteString -> Either String ByteString
