Corrected several coding mistakes