add debugging code and ensure logging for errors