bugfix and finished final code