Completed buildGivenN script