added error handling (NoMethodError, TypeError)