refractor the code and changed for new version