final implementation of parsing function