Code implemented from the Learning Javascript book by Tim Wright