fixed bugs #527 and 516. Added padding mechanism