re-organized code