updated code: modified performace and added logs