Corrected numerous bugs