Changed the ifelse statment to work correctly