 
   if bugs.turns % 10 == 0
     stats = bugs.names.map((name) -> bugs.stats[name])
-    make_chart '#jitterbug_progress_chart_svg', stats, 1000, bugs.count
-
-  if bugs.turns < 1000
+    make_chart '#jitterbug_progress_chart_svg', stats, bugs.max_turns, bugs.count
+
+  if bugs.turns < bugs.max_turns
     bugs.turns += 1
     setTimeout((-> next_turn bugs, canvas), 50)
 
-empty_grid = ->
+starting_bugs = (bug_size, width, height, max_turns) ->
   grid = []
 
-  for x in [0..59]
+  for x in [0..(width - 1)]
     grid[x] = []
-    for y in [0..39]
+    for y in [0..(height - 1)]
       grid[x][y] = null
 
-  grid
-
-starting_bugs = ->
   bugs =
-    grid:        empty_grid()
+    grid:        grid
     colors:      {}
     next_serial: 0
     stats:       {}

turn_bug = (bug, offset) ->
  bug.direction = (((bug.direction + offset) % 4) + 4) % 4

add_bug_to_canvas = (canvas, bug) ->
  x   = bug.location.x * 10
  y   = bug.location.y * 10
  pos = switch bug.direction
    when 0 then left: x,      top: y,      angle:   0
    when 1 then left: x + 10, top: y,      angle:  90
    when 2 then left: x + 10, top: y + 10, angle: 180
    when 3 then left: x,      top: y + 10, angle: 270

  if bug.prev_name == bug.name
    if bug.canvas_img

    fabric.Image.fromURL url, (img) ->
      bug.canvas_img = img
      canvas.add img.set
        width:      bug_size
        height:     bug_size
        left:       pos.left
        top:        pos.top
        angle:      pos.angle
