Fixed several fatal bugs