much better impl of #242