Some code cleanup, and added some docstrings