Adding timestamps to logging.

Also a bit of internal refactorings.