this could use some refactoring for better readability