Fix several bugs and errors found by slyfoxx