Code taken from original author's webpage.