Add some more debug output and integrate some changes from develop branch.