refactored and simplified the parsing function