organizing the loops and functions better