Format code to comply with PEP8

Also add/improve some docstrings.