Fixed small bugs in the logic