Added subprocess handling and fixed output reversal