This also removes all multi-threaded calls to the database as they were not working as expected.
The transactions are now used to keep the database in a sane state, but as far as possible log all messages from the connection to the database without using cross-thread requests.
Also fixed some errors with cleanup and rewrote some of the remote filename parsing and validation.