Added multi-buffering and removed a lot of boilerplate code.