Fix several bugs in DoB method and improve overall code quality