Refactored code; Added some logs for verification; Added checks to increase stability