Move this line before line 50 and compare directly to $w or $h