Fixes errors and bad output

Fixes #2
For more detail see commits.