ported over old code and cleaned it up