Make my code less script-kiddyish. restructured loop