Improve port handling
Add test suite for script generation