Improved resize function to share code with get_dimensions.