added trimspace and better error reporting