Cleanup and fixes suggested by @akeym