ES: cleaning up nasty nested code