Corrected the loop issue, code very very messy as of now