updated exception handling and added __len__