add more debug messages, clean up code