Add various bugfixes and debugging calls