Rewrite some code and make it work