Complete rewrite in an effort to gain performance