simplified code and removed test code.