Fixed error handling - thanks to kukoarmas