Adds functionality for set-nth.

Fixes #578 and #581.