Bugfix and tidy up