Tidied code, improved documentation