nio refactored, not yet debugged