cleaned up code. moved database-related things to seperate file.