Complete rewrite

This is a complete rewrite of the program