Refactored with mime support