Corrected several bugs