Updated to include better API handling