refactored and simplified parser