Moved logging to more logical places

Out of main() and into the functions