Bug fixes. Added logging for easier debugging.