Improved docstrings. Code cleanup and db query optimisation.