fixed caclucation logic