Improved format detection