Added better input/output handling.