Square will not move correctly within bounding box

https://codepen.io/jtog95/pen/wvMXyym

Hi I’m trying to get this square to move correctly in this box. I want the square to not be able to move off the screen at the bottom and the right. My problem is the square will not move correctly in the box, it will glitch and skip around and I’m having trouble figuring out whats wrong with the positioning.

I know it has something to do with this bit of code here.

function ensureBounds(sprite) {
    if(sprite.x < 20) {
        sprite.x = 20;
    }
    if(sprite.y < 20) {
        sprite.y = 20;
    }
    if (sprite.x + sprite.y > 480) {
         sprite.x = 480 - sprite.w;
     }
    if (sprite.y + sprite.h > 480) {
         sprite.y = 480 - sprite.h
     }
}