Fixed with port condition and cleaned up code
Renamed vars