refactored and fixed invalid data parsing