      {x3, y1}, {x3, y2}, {x3, y3}]
   end
 
-  def grid_elem(grid, x, y) do
-    elem(elem(grid, y), x)
+  def cell_alive?(world, {x, y}) do
+    elem world.grid, y * world.width + x
   end
 
-  def render(screen, grid, width, height) do
-    context = Screen.start_drawing(screen)
-    Screen.clear context
-    for y <- 0..(height-1), x <- 0..(width-1), grid_elem(grid, x, y) do
+  def render(context, world, cell_size, cell_color) do
+    for coord = {x, y} <- grid_coords(world), cell_alive?(world, coord) do
       Screen.draw_rectangle(
-        context, @cell_color,
-        x * @cell_size, y * @cell_size, @cell_size, @cell_size)
+        context, cell_color,
+        x * cell_size, y * cell_size, cell_size, cell_size)
     end
-    Screen.finish_drawing(context)
   end
 end
 

    :wxPen.destroy pen
    :wxBrush.destroy brush
  end

  def _create_window(title, width, height) do
    :wx.new()
    frame = :wxFrame.new(
      :wx.null, wxID_ANY, title, size: {width, height},
       style: wxSYSTEM_MENU ||| wxMINIMIZE_BOX  ||| wxCLOSE_BOX ||| wxCAPTION)
    :wxFrame.show frame
    frame
  end
end



  import Bitwise
  import WxConstants

  def init(title, width, height) when is_binary(title) do
    init to_charlist(title), width, height
  end
  def init(title, width, height) do
    :wx.new()
    frame = :wxFrame.new(
      :wx.null, wxID_ANY, title, size: {width, height},
       style: wxSYSTEM_MENU ||| wxMINIMIZE_BOX  ||| wxCLOSE_BOX ||| wxCAPTION)
    :wxFrame.show frame
    frame
  end

  def update(frame, bg_color, render) do
    # Create drawing context.
    dc = :wxBufferedPaintDC.new(frame)
    # Clear screen.
    :wxDC.setBackground dc, brush = :wxBrush.new(bg_color)
    :wxDC.clear dc
    :wxBrush.destroy brush
    # Call the render function.
    render.(dc)
    # This is required to make the changes visible.
    :wxBufferedPaintDC.destroy dc
  end

  def draw_rectangle(dc, color, x, y, width, height) do
