Fix logging
Clean up code slightly