added maxBuffer and did some tidying up