Implemented according to Programming Assignment 2 of Coursera rprog-006