Fixed error handling and tests