Changed implementation according to a suggestion by Rich Eakins.