Design optimizing and added some error treatment