Added foreign keys to simplify deletion, bugfixes