Bugfix and better error logging