Fixed resizing logic to include margin