First pass at grammify algorithm