seems to be working well after major refactoring