Add better error handling and performance tweaks