Fixed bugs. Initial version works