fixed issues and simplified code