Improved fix for "too long command line" error

Fixed tests, improved code.