tidied code and added some doc blocks