Corrected various bugs