Fixed logic errors and typos