modified code for S3