Fixed some problems causing incorrect output.