Add basic buffer manager and fix some bugs