Made compatible with Java 5.
Added error handling.