Clean up and use more of the api for robustness and debugging