compleate refactoring for multi port usage