replaced system.err with exception

Also replaced StringBuffer with StringBuilder