remformated and fixed some of the logic