Added some comments.
Moved database stuff to own module.