reorganizing the code and finish drawing a rectangle on the image