more cleanup. bug fix. improve error messages