Simplified the code be removing nested "where" statements