Fixed logic of code