Decoupled S3-related functions and cleaned up code