Final working code, without epsilon testing