updated __len__ and exception handling