working and somewhat optimized version using S3 and fewer pitfalls.