Modifications to fix the bugs; should be ready to tune.