some modifications and re-factoring