Refactored capture code and its working