Fixed all the issues. The code is working perfectly now