Corrected many bugs