Bugfix & error logging

See commits.