Added logic as required by coursera/rprog-004/assignment2