Cleaned the code and improved the for looop