Made buffering more stable, fixed bugs, code cleaning