     brewing =
       barista: barista
       dibs: [barista],
-      messageMetadata: msg.envelope.message.metadata,
+      messageMetadata: msg.envelope.message?.metadata,
 
     robot.brain.save()
 
     msg.send "@team Brew started by @#{brewing.barista}! #{statusEmoji.random('success')}\n" +
-      "To grab a spot use: #{robot.alias}dibs\n" +
-      "To end the brew use: #{robot.alias}fresh pot"
+        "To grab a spot use: #{robot.alias}dibs\n" +
+        "To end the brew use: #{robot.alias}fresh pot"
 
   endBrew = (msg) ->
-
     teamNotify = brewing.dibs.length < dibsLimit
 
-    expiry = new Date (new Date()).getTime() + dibsDuration*1000
+    expiry = new Date (new Date()).getTime() + dibsDuration * 1000
 
     claims = []
     for i in [1..dibsLimit]
-      dibber = brewing.dibs[i-1]
+      dibber = brewing.dibs[i - 1]
 
       if dibber
         client = robot.brain.userForName dibber
 
         if dibber == brewing.barista
-          coffeeconomy.deposit client, dibsLimit-1
+          coffeeconomy.deposit client, dibsLimit - 1
         else
           coffeeconomy.withdraw client, 1
 
         account = coffeeconomy.account client
 
-        claims.push("**@#{dibber}** " +
-          "#{if dibber == brewing.barista then '_(barista)_ :chart_with_upwards_trend:' else ':chart_with_downwards_trend:'} " +
-          "#{coffeeconomy.balanceFormatter account.balance}")
+        dibberLabel = "**@#{dibber}**"
+
+        if dibber == brewing.barista
+          txIcon = ":chart_with_upwards_trend:"
+          dibberLabel += " _(barista)_"
+        else
+          txIcon = ":chart_with_downwards_trend:"
+
+        claims.push("#{dibberLabel} #{txIcon} " +
+            "#{coffeeconomy.balanceFormatter account.balance}")
       else
         claims.push "_Unclaimed!_"
 
     threadMsg = threadedMsg(msg)
     threadMsg.send "#{if teamNotify then '@team: ' else ''}Fresh Pot!!! #{msg.random freshPots} " +
-      ":coffee: Claims (valid until #{dateformat expiry, 'h:MM:ss tt'}):\n" +
-      ("#{(parseInt index)+1}. #{claim}" for index, claim of claims).join("\n")
+        ":coffee: Claims (valid until #{dateformat expiry, 'h:MM:ss tt'}):\n" +
+        ("#{(parseInt index) + 1}. #{claim}" for index, claim of claims).join("\n")
 
     if claims.length > 1
       setTimeout ->
         threadMsg.send "Claims have expired!"
-      , dibsDuration*1000
+      , dibsDuration * 1000
 
     brewing = {}
     robot.brain.save()

      account.totals.withdrawn -= amount
    account.transactions++
    @saveAccount account
    @robot.logger.info "#{amount} :coffee:s #{if amount > 0 then 'deposited to' else 'withdrawn from'} #{client.name} (#{client.id})"

  balance: (client) ->
    (@account client).balance

#   hubot coffee balance [user] - Check your coffee balance (defaults to you)
#   hubot coffee top [n] - See the top coffee balances (defaults to 10)
#   hubot coffee bottom [n] - See the bottom coffee balances (defaults to 10)
#
#
# Author:
#   brent@kiratalent.com, Kira Talent Inc.

dateformat = require 'dateformat'

dibsLimit = process.env.HUBOT_COFFEE_DIBS_LIMIT || 6
dibsDuration = process.env.HUBOT_COFFEE_DIBS_DURATION || (60 * 10)

class StatusEmoji
  success: [':dancer:', ':relaxed:', ':grin:', ':smile:', ':sunglasses:'],
  pending: [':worried:', ':see_no_evil:', ':no_mouth:', ':neutral_face:']
  failure: [':cry:', ':angry:', ':fearful:', ':sob:', ':disappointed:']

  random: (status) ->
    emojis = @[status] || []
    return emojis[Math.floor(Math.random() * emojis.length)]

freshPots = [
  "http://stream1.gifsoup.com/view6/3131142/chug-coffee-o.gif",
