Bugfixing logic error