Fixed many important bugs