Updated code for Coursera programming assignment 2