This also shuffles the code around a little more to cluster the error checking and separately cluster the returns