moved level modification outside of nested function