Bugfix to previous commit.
Delayed database write to avoid deadlocks.