code is not 100% good state need to review this again tomorrow.