Added buffer; some refactoring.