fix padding function to use less memory