     password = new Buffer(password)
 
   if typeof salt is 'string'
-    salt = new Buffer(salt+SALT_SUFFIX, 'hex')
+    salt = new Buffer(salt + SALT_SUFFIX, 'hex')
   else
     salt = Buffer.concat [salt, new Buffer(SALT_SUFFIX, 'hex')]
 
   hmac = new Hmac(password, length)
   last = xorsum = hmac.encrypt(salt)
-  bit_length = 512 / 8
+  bit_length = length / 8
+
   i = 1
   while i < count
     xorsum = hmac.encrypt(xorsum)


###*
 * PBKDF2
 * @param  {string|buffer} password      The password to derive a key from.
 * @param  {string|buffer} salt          The salt.
 * @param  {number} [count=10000] Number of iterations.
 * @param  {number} [length=512]  The SHA algorithm to use.
 * @return {buffer}               The derived key.
###
module.exports = (password, salt, count=10000, length=512) ->


      last[j] ^= xorsum[j]
      j++
    i++

  return last.toString('hex')
