refactoring the code that calculates the bounding box for an image