refactored loop, could be even better probably