Rewrite packet reading/parsing code

This way (read and parse each packet instead of reading all packets and only then parsing) uses considerably less memory