Major changes to logics. Now correctly working