Remove trailing spaces and switch envelope calculation to table lookup