Fix some IO bugs
Improve IO functions