fixing loop problem more elegant