bits borrowed from Square's ETL gem