whitespace cleanup and better error checking