some improvements from mpdehaan's review