Added few comments and now tried to optimize the code for better working