Added extension detection and file opening