fix inserting tags into an empty section triggering an error message; minor optimizations