Optimized code & Modified version.