Further improvements suggested by felixhayashi