added gzip compression and rearranged functions