refactored ugly parse method