Add logging, fix small bugs