ported to jdk 1.6, more error handling