updated to include credit to Nicolas Rougier