Fixed output and added some basic checking