Added preconditions checking and Javadoc.