nn: refactoring and fixes