added testing and more robust error handeling