add debugging and error checking