Adding preconditions checks and doing some reformatting. Also added some Javadocs (more to come).