Fixed bugs and implemened bdwrite()