Bugfix and small code clean-up