Refactored the image capture code for improved speed