Completed my added TODO statements and did some changes to improve the performance