Why not use something like OpenX? It's free, it's been developed on for years (I remember when it was phpmyads), and works great.