Improvements by @hvr

@hvr: can I pull your changes? They seem useful.