Fixing buffer bug, and cleanup