Improved code, reduced if-else, treat multiple whitespaces as one