complete refactor; next is to add nested foreach and the remaining relational operators