 		  &num_bytes,
 		  *vbo_resource) );
 
-  launchKernel(positions, mesh_width, mesh_height, sim_time); 
+  launchKernel(positions, mesh_width, mesh_height, sim_time);
   // Unmap Buffer
+  fprintf(stdout, "   unmapping buffer\n");
   checkCudaErrors( cudaGraphicsUnmapResources(1, vbo_resource, 0) );
+  fprintf(stdout, "     x:%f  y:%f  z:%f w:%f\n", &positions[10].x, &positions[10].y, &positions[10].z, &positions[10].w);
+}
+
+__host__ void createVBO(GLuint *vbo, struct cudaGraphicsResource **vbo_res, unsigned int vbo_res_flags) {
+  fprintf(stdout, "   creating vbo\n");
+  assert(vbo);
+  // Create buffer object and register it with CUDA
+  glGenBuffers(1, vbo);
+  glBindBuffer(GL_ARRAY_BUFFER, *vbo);
+  unsigned int size = mesh_width * mesh_height * 4 * sizeof(float);
+  glBufferData(GL_ARRAY_BUFFER, size, 0, GL_DYNAMIC_DRAW);
+
+  // Unbind Buffer
+  glBindBuffer(GL_ARRAY_BUFFER, 0);
+
+  // Register VBO
+  checkCudaErrors( cudaGraphicsGLRegisterBuffer(vbo_res, *vbo, vbo_res_flags) );
+}
+
+__host__ void deleteVBO(GLuint *vbo, struct cudaGraphicsResource *vbo_res) {
+  fprintf(stdout, "   deleting vbo\n");
+  // Unregister with CUDA
+  checkCudaErrors( cudaGraphicsUnregisterResource(vbo_res) );
+
+  glBindBuffer(1, *vbo);
+  glDeleteBuffers(1, vbo);
+
+  *vbo = 0;
 }
 
 __host__ void Render() {
-  
+
   // Launch Kernel
   runCUDA(&positionsVBO_CUDA);
 
   // Clear screen
   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
-  
+
   // Set View Matrix
   glMatrixMode(GL_MODELVIEW);
   glLoadIdentity();

  glViewport(0, 0, width, height);
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  gluPerspective(45.0, (double)width / (double)height, 1.0, 200.0);
}

__host__ void GLInit(int argc, char* argv[]) {

  // Create Window
  glutInit(&argc, argv);
  glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH );
  glutInitWindowSize(window_width, window_height);
  glutInitWindowPosition(0, 0);
  glutCreateWindow("cuda-boids");

  glutReshapeFunc(windowResize);
  glutDisplayFunc(Render);
  glutSpecialFunc(Keyboard);

  // Allow Depth and Colors

__host__ void launchKernel(float4* positions, unsigned int width, unsigned int height, float t) {
  dim3 dimBlock(16, 16, 1);
  dim3 dimGrid(width / dimBlock.x, height / dimBlock.y, 1);
  fprintf(stdout, "   launching kernel\n");
  kernel<<<dimGrid, dimBlock>>>(positions, width, height, t);
  fprintf(stdout, "   exiting kernel\n");
}

__host__ void runCUDA(struct cudaGraphicsResource** vbo_resource) {
  fprintf(stdout, "   running cuda\n");
  // Map VBO to GL with CUDA
  float4* positions;
  checkCudaErrors( cudaGraphicsMapResources(1, vbo_resource, 0) );
