Added support for gzip
Simplified logic a bit