Final update with error handling, no leaks