Complete rewrite, make player a library function