More principled logging
Fixed several bugs