Improve logging
Regorganize code