Move more code into lib's and rewrote serialize