Add perpetual code

@mortenvp @janusheide I will try to update this branch with the master. We should release perpetual NC soon.
Ok, this is complicated.. I think we need a bit more description on what this function does..