Fixed code to handle arbitrary file lengths without hardcoding months