Fixes to nn (rebased)

This is rebased #1481