fixed some errors in logic