mabye use buffer.length in the loop just to be on the safe side for further refactoring ?