First commit, major rewrite of code