Corrected bugs but code needs to be cleaned