A more elegant solution suggested by zlorf.