Added log file functions
Cleaned up code structure