Fix a domain error and incorrect calculation in padding