Fixed various important bugs