Fixed several bugs