Improved Pad function.