massive refactor and serializing to byte arrays