modified file with complete logic