Fix several bugs