Refactor buffered Javascript