Updated to fix logic issues