Improved error handeling