Added support for negative residue numbers
Added call to flush stdout to prevent BrokenPipe errors