Fixing function logic