Add more logging and some bug fixing