works, with extra print statements(to be removed)