Updated logic to avoid Cross-Origin Policy Problems