adapted some stuff from the tutorials/samples