a few small bugfixes
Variation on http://stackoverflow.com/a/16528383/680454