greatly improved structure of code