Code from compdata class at coursera.org