Cleaned up code, logging, and documented all functions that needed it.