a complete rewrite of the whole system. now working.