various improvements mentioned in comments